package org.dtrd.modules.patient.entity.po;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.JdbcType;
import org.dtrd.common.util.DateUtils;
import org.dtrd.common.util.MD5Util;
import org.dtrd.config.system.Constant;
import org.dtrd.config.wechat.entity.bean.officialaccount.WechatOfficialAccountUserInfo;
import org.dtrd.modules.patient.entity.bean.PatientInfo;
import org.dtrd.modules.patient.entity.bean.PatientPublicAccountInfo;
import org.dtrd.modules.questionnaire.entity.bean.PatientBasicInfo;
import org.dtrd.modules.questionnaire.entity.bean.QuestionnaireRdVersionDetail;
import org.dtrd.modules.questionnaire.entity.bean.QuestionnaireLifewayDetail;

import javax.persistence.*;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Date;

@Data
@NoArgsConstructor
@Table(name = "dtrd_ent_rd_patient")
public class DtrdEntRdPatient {
    @Id
    @Column(name = "data_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.AUTO)
    private Integer dataId;

    @TableField("doctor_id")
    private Integer doctorId;

    /**
     * 手机号码
     */
    @Column(name = "patients_mobile")
    private String patientsMobile;

    /**
     * 唯一标识
     */
    @Column(name = "patients_uuid")
    private String patientsUuid;

    /**
     * 登录密码
     */
    @Column(name = "patients_password")
    private String patientsPassword;

    /**
     * 患者姓名
     */
    @Column(name = "patients_name")
    private String patientsName;

    /**
     * 用户昵称
     */
    @Column(name = "patients_nickname")
    private String patientsNickname;

    /**
     * 出生日期
     */
    @Column(name = "patients_birthday")
    private Date patientsBirthday;

    /**
     * 性别
     */
    @Column(name = "patients_sex")
    private Integer patientsSex;

    /**
     * 身份证号
     */
    @Column(name = "patients_id_card")
    private String patientsIdCard;

    /**
     * 身高
     */
    @Column(name = "patients_height")
    @TableField(value = "patients_height", updateStrategy = FieldStrategy.IGNORED)
    private Integer patientsHeight;

    /**
     * 体重
     */
    @Column(name = "patients_weight")
    @TableField(value = "patients_weight", updateStrategy = FieldStrategy.IGNORED)
    private Float patientsWeight;

    /**
     * 头像图片地址
     */
    @Column(name = "patients_img")
    private String patientsImg;

    /**
     * 糖尿病类型
     */
    @Column(name = "patients_diabetis_type")
    private Integer patientsDiabetisType;

    /**
     * 确诊日期
     */
    @Column(name = "patients_confirmed_date")
    private Date patientsConfirmedDate;

    /**
     * 起病方式
     */
    @Column(name = "patients_mode_onset")
    private String patientsModeOnset;

    /**
     * 起病症状
     */
    @Column(name = "patients_symptoms")
    private String patientsSymptoms;

    /**
     * 医保类型
     */
    @Column(name = "patients_medical_insurance")
    private String patientsMedicalInsurance;

    /**
     * 诊断结果
     */
    @Column(name = "patients_diagnostic")
    private String patientsDiagnostic;

    /**
     * 患者位置
     */
    @Column(name = "patients_location")
    private String patientsLocation;

    /**
     * 所属推广渠道Id
     */
    @Column(name = "channel_id")
    private Integer channelId;

    /**
     * 患者所属省份Id
     */
    @Column(name = "patients_province_id")
    private Integer patientsProvinceId;

    /**
     * 患者所属省份名称
     */
    @Column(name = "patients_province_name")
    private String patientsProvinceName;

    /**
     * 患者所属城市Id
     */
    @TableField(value = "patients_city_id",
            updateStrategy = FieldStrategy.IGNORED,
            insertStrategy = FieldStrategy.IGNORED,
            jdbcType = JdbcType.INTEGER)
    private Integer patientsCityId;

    /**
     * 患者所属城市名称
     */
    @Column(name = "patients_city_name")
    private String patientsCityName;

    /**
     * 状态
     */
    @Column(name = "patients_status")
    private Integer patientsStatus;

    /**
     * 类型
     */
    @Column(name = "patients_type")
    private Integer patientsType;

    /**
     * 患者名称首字母大写
     */
    @Column(name = "patients_initial")
    private String patientsInitial;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

    @TableLogic
    @Column(name = "is_del")
    private Integer isDel;

    /**
     * 0-OMS,1-APP,2-数治健康小程序，3-工具箱小程序
     */
    @Column(name = "patients_client_type")
    private Integer patientsClientType;

    /**
     * 企业微信客户Id
     */
    @Column(name = "patients_qywx_external_userid")
    private String patientsQywxExternalUserid;

    /**
     * 企业微信成员id
     */
    @Column(name = "patients_qywx_userid")
    private String patientsQywxUserid;

    /**
     * 小程序微信openID
     */
    @Column(name = "wx_openid")
    private String wxOpenid;

    /**
     * 微信的unionid
     */
    @TableField("wx_unionid")
    private String wxUnionid;

    /**
     * 微信公众号openid
     */
    @TableField("wx_oa_openid")
    private String wxOaOpenid;

    @Column(name = "机构编码")
    private String orgCode;

    /**
     * 患者主诉
     */
    private String patientsDescription;

    /**
     * 复诊接待码
     */
    private String flupReceptionQrcode;

    /**
     * 复诊结束码
     */
    private String flupFinishQrcode;

    /**
     * 患者建档基本信息是否完成
     */
    private Integer isFinished;

    @TableField("wx_oa_qrcode_url")
    private String wxOaQrcodeUrl;

    @TableField("wx_oa_qrcode_source_url")
    private String wxOaQrcodeSourceUrl;

    @TableField("wx_tools_openid")
    private String wxToolsOpenid;

    public DtrdEntRdPatient(String wxUnionid, String wxOaOpenid) {
        this.wxUnionid = wxUnionid;
        this.wxOaOpenid = wxOaOpenid;
    }

    public DtrdEntRdPatient init() {
        isDel = 0;
        createTime = new Date();
        updateTime = new Date();
        patientsPassword = MD5Util.MD5Encode(Constant.DEFAULT_PWD, StandardCharsets.UTF_8.name());
        patientsStatus = 0;
        orgCode = Constant.DEFAULT_ORGCODE;
        return this;
    }

    public DtrdEntRdPatient parseFromDTO(PatientPublicAccountInfo dto) {
        dataId = dto.getPatientId();
        patientsName = dto.getPatientsName();
        patientsImg = dto.getPatientsImg();
        patientsBirthday = dto.getPatientsBirthday();
        patientsCityId = dto.getCityId();
        patientsProvinceId = dto.getProvinceId();
        patientsSex = dto.getPatientsSex();
        patientsSymptoms = dto.getPatientsSymptoms();
        patientsConfirmedDate = dto.getPatientsConfirmedDate();
        patientsNickname = dto.getPatientsNickname();
        patientsIdCard = dto.getPatientsIdcard();
        patientsHeight = dto.getPatientsHeight();
        patientsWeight = dto.getPatientsWeight();
        return this;
    }

    public DtrdEntRdPatient parseFromDTO(PatientBasicInfo dto) {
        dataId = dto.getPatientId();
        patientsName = dto.getName();
        patientsSex = dto.getSex();
        patientsDiabetisType = dto.getDiabetesType();
        patientsMobile = dto.getPhone();
        patientsBirthday = dto.getBirthday();
        isFinished = Constant.FINISHED;
        return this;
    }

    public DtrdEntRdPatient parseFromDto(PatientInfo dto) {
        dataId = dto.getDataId();
        if (dto.getPatientsNickname() != null) {
            patientsNickname = dto.getPatientsNickname();
        }
        if (dto.getPatientsName() != null) {
            patientsName = dto.getPatientsName();
        }
        if (dto.getPatientsImg() != null) {
            patientsImg = dto.getPatientsImg();
        }
        try {
            if (dto.getPatientsBirthday() != null) {
                patientsBirthday = DateUtils.parseDate(dto.getPatientsBirthday(), DateUtils.date_sdf.get().toPattern());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dto.getPatientsSex() != null) {
            patientsSex = dto.getPatientsSex();
        }
        if (dto.getPatientsIdCard() != null) {
            patientsIdCard = dto.getPatientsIdCard();
        }

        if (!dto.getUpdateDiabetes()) {
            patientsWeight = dto.getPatientsWeight();
            patientsHeight = dto.getPatientsHeight();
        }

        if (dto.getPatientsDiabetisType() != null) {
            patientsDiabetisType = dto.getPatientsDiabetisType();
        }
        if (dto.getPatientsModeOnset() != null) {
            patientsModeOnset = dto.getPatientsModeOnset();
        }
        if (dto.getPatientsSymptoms() != null) {
            patientsSymptoms = dto.getPatientsSymptoms();
        }
        if (dto.getPatientsDiagnostic() != null) {
            patientsDiagnostic = dto.getPatientsDiagnostic();
        }
        try {
            if (dto.getPatientsConfirmedDate() != null) {
                patientsConfirmedDate = DateUtils.parseDate(dto.getPatientsConfirmedDate(), DateUtils.date_sdf.get().toPattern());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dto.getPatientsMedicalInsurance() != null) {
            patientsMedicalInsurance = dto.getPatientsMedicalInsurance();
        }
        if (dto.getWxOpenid() != null) {
            wxOpenid = dto.getWxOpenid();
        }
        return this;
    }

    public DtrdEntRdPatient parseFromDTO(QuestionnaireRdVersionDetail dto) {
        dataId = dto.getPatientId();
        patientsName = dto.getName();
        patientsSex = dto.getSex();
        return this;
    }

    public DtrdEntRdPatient parseFromDTO(QuestionnaireLifewayDetail dto) {
        dataId = dto.getDataId();
        patientsMobile = dto.getPhone();
        patientsName = dto.getName();
        patientsSex = dto.getSex();
        return this;
    }

    public DtrdEntRdPatient parseFromDTO(WechatOfficialAccountUserInfo userInfo) {
        patientsName = userInfo.getNickname();
        wxOaOpenid = userInfo.getOpenid();
        wxUnionid = userInfo.getUnionid();
        patientsSex = userInfo.getSex();
        patientsProvinceName = userInfo.getProvince();
        patientsCityName = userInfo.getCity();
        patientsImg = userInfo.getHeadimgurl();
        return this;
    }

    @Override
    public String toString() {
        return "DtrdEntRdPatient{" +
                "dataId=" + dataId +
                ", doctorId=" + doctorId +
                ", patientsMobile='" + patientsMobile + '\'' +
                ", patientsName='" + patientsName + '\'' +
                ", patientsBirthday=" + patientsBirthday +
                ", patientsSex=" + patientsSex +
                ", patientsHeight=" + patientsHeight +
                ", patientsWeight=" + patientsWeight +
                ", patientsDiabetisType=" + patientsDiabetisType +
                ", wxOpenid='" + wxOpenid + '\'' +
                ", wxUnionid='" + wxUnionid + '\'' +
                ", wxOaOpenid='" + wxOaOpenid + '\'' +
                ", orgCode='" + orgCode + '\'' +
                '}';
    }
}